package com.haieros.www.purerunshowsystem;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.TextView;

import com.haieros.www.purerunshowsystem.utils.SDUtils;
import com.haieros.www.purerunshowsystem.utils.SharedPreferencesUtils;
import com.haieros.www.purerunshowsystem.utils.VideoViewUtil;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.io.File;
import java.io.IOException;


/**
 * Created by admin on 2017/11/17.
 */

public class StartActivity extends BaseAcivity {
    private ImageLoader imageLoader;
    private VideoViewUtil mVideoView;
    private Context context=this;
    private Uri uri;
    private String path = "/etc/TPV/video/cjpsm.mp4";
    private SDUtils sdUtils;
   private TextView mTextDetroy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //定义全屏参数
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //获得当前窗体对象
        Window window = StartActivity.this.getWindow();
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
        setContentView(R.layout.activity_start);
        Log.e("TAG",this.getLocalClassName()+" OnCreate");
        //第一次进入应用播放
        if(!(boolean) SharedPreferencesUtils.getParam(this,"type",false)) {
            SharedPreferencesUtils.setParam(this, "type", true);
            initView();
        }else
        {
            Intent intent=new Intent(this,MainActivity.class);
            startActivity(intent);
            finish();
        }

    }

    @Override
    public void onBackPressed() {
        return;
    }


    private void initView() {
        //结束Text初始化
        mTextDetroy=findViewById(R.id.text_destory);
        mTextDetroy.requestFocus();
        mTextDetroy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("TAG","点击结束了");
                finish();
                Intent intent=new Intent(getApplicationContext(),MainActivity.class);
                startActivity(intent);
            }
        });
        // 流媒体操作类
        mVideoView= (VideoViewUtil) findViewById(R.id.videoview_start);
        //设置视频控制器
//        MediaController mc = new MediaController(this);
//        mc.setVisibility(View.INVISIBLE);
//        mVideoView.setMediaController(mc);
        //播放完成回调
        mVideoView.setOnCompletionListener( new MyPlayerOnCompletionListener());
        Log.e("TAG", "initView: "+Environment.getDataDirectory().getPath());
       uri=Uri.parse(path);
        //设置视频路径
        mVideoView.setVideoURI(uri);
        //开始播放视频
        mVideoView.start();
    }

    class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {

        @Override
        public void onCompletion(MediaPlayer mp) {

            finish();
            Intent intent=new Intent(getApplicationContext(),MainActivity.class);
            startActivity(intent);

        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("TAG",this.getLocalClassName()+" OnDestroy");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e("TAG", "onKeyDown:-- " + keyCode);
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e("TAG", "onTouchEvent:-- ");
        return true;
    }
}
